<?php

namespace app\controllers;

use Yii;
use app\models\GkyWeArticlePraise;
use app\models\GkyWeArticlePraiseSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * GkyWeArticlePraiseController implements the CRUD actions for GkyWeArticlePraise model.
 */
class GkyWeArticlePraiseController extends Controller {
    public $enableCsrfValidation = false;
    public function behaviors() {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all GkyWeArticlePraise models.
     * @return mixed
     */
    public function actionIndex() {
        $searchModel = new GkyWeArticlePraiseSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
                    'searchModel' => $searchModel,
                    'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single GkyWeArticlePraise model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id) {
        return $this->render('view', [
                    'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new GkyWeArticlePraise model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new GkyWeArticlePraise();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                        'model' => $model,
            ]);
        }
    }
    public function actionPraise($artid){
        $result = GkyWeArticlePraise::findOne(['articel_id' => intval($artid)]);
        $result->count+=1;
        if($result->save()){
            header("Access-Control-Allow-Origin: *");
            echo 1;
        }else{
            header("Access-Control-Allow-Origin: *");
            echo -1;
        }
    }
    public function actionShow($artid) {
        $result = GkyWeArticlePraise::findOne(['articel_id' => intval($artid)]);

        if (isset($result->id)) {
            $result->read_count+=1;
            $result->save();
            $res = (new \yii\db\Query())
                    ->select('*')
                    ->from('gky_we_article_praise')
                    ->where(['articel_id' => intval($artid)])
                    ->one();
            header("Access-Control-Allow-Origin: *");
            echo json_encode($res);
        } else {
            $model = new GkyWeArticlePraise();
            $model->articel_id = $artid;
            $model->read_count = 1;
            if ($model->save()) {
                $result = (new \yii\db\Query())
                        ->select('*')
                        ->from('gky_we_article_praise')
                        ->where(['articel_id' => intval($artid)])
                        ->one();
                header("Access-Control-Allow-Origin: *");
                echo json_encode($result);
            } else {
                echo '-------';
            }
        }
    }

    /**
     * Updates an existing GkyWeArticlePraise model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id) {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                        'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing GkyWeArticlePraise model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id) {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the GkyWeArticlePraise model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return GkyWeArticlePraise the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = GkyWeArticlePraise::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

}
